package com.ubst.smartad.login;

import java.io.*;
import java.sql.*;
import java.util.*;

import javax.servlet.*;
import javax.servlet.http.*;

import com.ubst.smartad.dbpool.TransactionManager;
import com.ubst.smartad.dao.LogDataDO;
import com.ubst.smartad.env.FileHostEnv;
import com.ubst.smartad.session.SessionChecker;
import com.ubst.smartad.common.FileUtil;
import com.ubst.smartad.common.TimeUtil;
import com.ubst.smartad.common.IpChecker;
import com.ubst.smartad.impl.LogDataMngImpl;
import com.ubst.smartad.logger.Debug;

public class LoginServlet extends HttpServlet {

	protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException,java.io.UnsupportedEncodingException
	{
		doPost(request ,response);
	}

    public void doPost(HttpServletRequest request ,HttpServletResponse response) throws ServletException,IOException {
		HttpSession session = request.getSession(false);
		String serverName = request.getServerName();
		FileHostEnv fhostEnv = FileHostEnv.getInstance(serverName);

		Debug debug = new Debug("LoginServlet", fhostEnv.getLogLevel());

		String user_home = FileUtil.getFilePath(System.getProperty("user.home"));
		/*
		String invalidLicense = (String)session.getAttribute("INVALIDLICENSE");
		if(invalidLicense==null || !invalidLicense.equals("TRUE")){
			response.sendRedirect( "/jsp/err/error.jsp?errcode=10" );
			return;
		}
		String invalidIP = (String)session.getAttribute("INVALIDIP");
		if(invalidIP==null || !invalidIP.equals(request.getRemoteAddr())){
			response.sendRedirect( "/jsp/err/error.jsp?errcode=11" );
			return;
		}
		*/

		String db_type = fhostEnv.getDBType();
		String data_sec = fhostEnv.getDataSec();

		TransactionManager tm = new TransactionManager();
		String userid = null;
		String passwd = null;

		try{
			if ( request.getParameter("login_id") != null){
				userid = request.getParameter("login_id").toLowerCase();
			}
			
			if ( request.getParameter("login_pw") != null){
				passwd = request.getParameter("login_pw");
			}

			if(userid != null && passwd != null){
				//FileUserMngImpl fileuserMng = new FileUserMngImpl(tm.connection ,serverName, fhostEnv.getLogLevel());
				LogDataMngImpl logMng = new LogDataMngImpl(tm.connection ,serverName, fhostEnv.getLogLevel());
				LogDataDO logdata = null;

				logdata = new LogDataDO();
				logdata.domain = serverName;
				logdata.user_id = userid;
				//logdata.org_code = fileuser.org_code;
				logdata.file_name = "NOFILE";
				logdata.file_size = 0;
				logdata.user_ip = request.getRemoteAddr();
				logdata.log_type = "LI";
				logdata.fdir_name = "-";

				session.setAttribute( "LOGIN_TYPE", "NORMAL" ); // 일반 로그인
				session.setAttribute( "INVALIDID", userid);	// 세션체크시 필요
				session.setAttribute( "USER_ID", userid );
				//session.setAttribute( "USER_LEVEL", String.valueOf(fileuser.user_level) );
				//session.setAttribute( "USER_NAME", fileuser.user_name);
				//session.setAttribute( "ORG_CODE", fileuser.org_code);
				session.setAttribute( "CONNECTIP", request.getRemoteAddr() );
				/*
				if(fileuser.user_level>=50){	// 중간관리자 이상
					session.setAttribute( "ISADMIN", true );
				}else{
					session.setAttribute( "ISADMIN", false );
				}
				*/
				session.setAttribute( "ISADMIN", true );

				String LOGINDATE = TimeUtil.getDateString(TimeUtil.getCurrentTime(),"yyyy-MM-dd HH:mm:ss"); //yyyy-MM-dd HH:mm:ss 형식
				session.setAttribute( "LOGIN_DATE", LOGINDATE );

				// SKIN 설정 정보를 세션에 넣는다.
				/*
				if( fileuser.user_skin==null || fileuser.user_skin.equals("") ){
					//값이 설정되어 있지 않으면 기본 설정을 세팅한다.
					fileuser.user_skin="skin1";
				}
				session.setAttribute( "USER_SKIN", fileuser.user_skin );
				*/
				session.setAttribute( "USER_SKIN", "skin1" );

				int session_time = fhostEnv.getSessionTime();
				if(session_time>0){
					session.setMaxInactiveInterval(session_time*60);	// 분단위
				}
				SessionChecker.addSession( userid, session );

				System.out.println("<!> Login Success : "+userid+"["+LOGINDATE+"]");

				response.sendRedirect( "/jsp/admin/status/manage/manage.jsp");

			}else{
				//-- 잘못된 데이타 오류
	            response.sendRedirect( "/jsp/err/error.jsp?errcode=1007");
				return;
			}
			
		}catch (Exception e){
			e.printStackTrace();
			response.sendRedirect("/jsp/err/error.jsp?errcode=2000");
		}finally{
			if(tm != null){
				try{tm.freeConnection();}catch(Exception e1){}
			}
		}

	}

}
